package com.mediacloud.app.newsmodule.fragment.military;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;
import com.mediacloud.app.newsmodule.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CustomMonthView extends MonthView {


    private int offsetTB;
    private int offsetLR;
    private RectF dst;
    protected Paint transparentPaint = new Paint();
    DecimalFormat decimalFormat = new DecimalFormat("00");

    public CustomMonthView(Context context) {
        super(context);

        dst = new RectF();
        mSelectTextPaint.setFakeBoldText(false);
        mSchemeTextPaint.setFakeBoldText(false);
        mCurMonthTextPaint.setFakeBoldText(false);
        mOtherMonthTextPaint.setFakeBoldText(false);
        offsetTB = dipToPx(getContext(), 4);
        offsetLR = dipToPx(getContext(), 6);
        transparentPaint.setColor(Color.TRANSPARENT);
    }

    /**
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param y         日历Card y起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return true 则绘制onDrawScheme，因为这里背景色不是是互斥的
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        mSelectedPaint.setStyle(Paint.Style.FILL);
        //   canvas.drawRect(x + mPadding, y + mPadding, x + mItemWidth - mPadding, y + mItemHeight - mPadding, mSelectedPaint);

        dst.left = x + offsetLR;
        dst.top = y + offsetTB;
        dst.right = x + mItemWidth - offsetLR;
        dst.bottom = y + mItemHeight - offsetTB;
      //  canvas.drawRect(dst, mSelectedPaint);
        float radius = getContext().getResources().getDimension(R.dimen.dimen6);
        canvas.drawRoundRect(dst,radius,radius,mSelectedPaint);

        return true;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
    }


    @SuppressWarnings("IntegerDivisionInFloatingPointContext")
    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        int top = y;
        if (isSelected && hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top, mSelectTextPaint);
        } else {
            Paint paint;
            if (hasScheme) {
                paint = calendar.isCurrentMonth() ? mCurMonthTextPaint : transparentPaint;
            } else {
                paint = calendar.isCurrentMonth() ? mOtherMonthTextPaint : transparentPaint;
            }
            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top, paint);
        }
    }

    int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}